package com.novabluetooth.adapter;

import java.util.ArrayList;
import java.util.List;

import com.health.R;
import com.health.tablebean.SBCL_DATA;
import com.novabluetooth.services.BluetoothConstant;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * 设备数据结果
 * 
 * @author nova
 *
 */
public class ExamHistoryAdaptor extends BaseAdapter {

	private static final String TAG = "ExamHistoryAdaptor";
	private List<SBCL_DATA> list;
	private List<SBCL_DATA> gaoList;
	private List<SBCL_DATA> diList;
	private List<SBCL_DATA> xinlvList;
	private Context context;
	private String flag;

	private List<SBCL_DATA> niaodanyuanList;
	private List<SBCL_DATA> qianxueList;
	private List<SBCL_DATA> danHongsuList;
	private List<SBCL_DATA> tongtiList;
	private List<SBCL_DATA> putaotangList;
	private List<SBCL_DATA> danbaizhiList;
	private List<SBCL_DATA> pHList;
	private List<SBCL_DATA> yaxiaoList;
	private List<SBCL_DATA> baixibaoList;
	private List<SBCL_DATA> bizhongList;
	private List<SBCL_DATA> weishengsuList;

	public ExamHistoryAdaptor(List<SBCL_DATA> list, Context context, String flag) {
		super();
		this.list = list;
		this.context = context;
		this.flag = flag;
	}

	public ExamHistoryAdaptor(List<SBCL_DATA> gaoList, List<SBCL_DATA> diList, List<SBCL_DATA> xinlvList,
			Context context, String flag) {
		super();
		if (gaoList.size() * diList.size() * xinlvList.size() == 0) {
			return;
		}
		this.gaoList = gaoList;
		this.diList = diList;
		this.xinlvList = xinlvList;
		this.context = context;
		if (gaoList.size() == 0) {
			this.list = new ArrayList<>();
		} else {
			this.list = gaoList;
		}

		this.flag = flag;
	}

	public ExamHistoryAdaptor(String flag, Context context, List<SBCL_DATA> niaodanyuanList,
			List<SBCL_DATA> qianxueList, List<SBCL_DATA> danHongsuList, List<SBCL_DATA> tongtiList,
			List<SBCL_DATA> putaotangList, List<SBCL_DATA> danbaizhiList, List<SBCL_DATA> pHList,
			List<SBCL_DATA> yaxiaoList, List<SBCL_DATA> baixibaoList, List<SBCL_DATA> bizhongList,
			List<SBCL_DATA> weishengsuList) {
		super();
		this.flag = flag;
		this.context = context;
		this.niaodanyuanList = niaodanyuanList;
		this.qianxueList = qianxueList;
		this.danHongsuList = danHongsuList;
		this.tongtiList = tongtiList;
		this.putaotangList = putaotangList;
		this.danbaizhiList = danbaizhiList;
		this.pHList = pHList;
		this.yaxiaoList = yaxiaoList;
		this.baixibaoList = baixibaoList;
		this.bizhongList = bizhongList;
		this.weishengsuList = weishengsuList;
		if (niaodanyuanList.size() == 0) {
			this.list = new ArrayList<>();
		} else {
			this.list = niaodanyuanList;
		}
	}

	@Override
	public int getCount() {
		if (list == null) {
			return 0;
		}
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh;
		SBCL_DATA data = null;
		SBCL_DATA gaodata = null;
		SBCL_DATA didata = null;
		SBCL_DATA xinlvdata = null;

		SBCL_DATA niaodanyuan = null;
		SBCL_DATA qianxue = null;
		SBCL_DATA danhongsu = null;
		SBCL_DATA tongti = null;
		SBCL_DATA putaotang = null;
		SBCL_DATA danbaizhi = null;
		SBCL_DATA pH = null;
		SBCL_DATA yaxiao = null;
		SBCL_DATA baixibao = null;
		SBCL_DATA bizhong = null;
		SBCL_DATA weishengsu = null;

		if (BluetoothConstant.XUEYA_TYPENAME.equals(flag)) {
			didata = diList.get(position);
			gaodata = gaoList.get(position);
			xinlvdata = xinlvList.get(position);
		} else if (BluetoothConstant.NIAOCHANGGUI_TYPENAME.equals(flag)) {
			niaodanyuan = niaodanyuanList.get(position);
			qianxue = qianxueList.get(position);
			danhongsu = danHongsuList.get(position);
			tongti = tongtiList.get(position);
			putaotang = putaotangList.get(position);
			danbaizhi = danbaizhiList.get(position);
			pH = pHList.get(position);
			yaxiao = yaxiaoList.get(position);
			baixibao = baixibaoList.get(position);
			bizhong = bizhongList.get(position);
			weishengsu = weishengsuList.get(position);
		} else {
			data = list.get(position);

		}

		if (convertView == null) {
			convertView = View.inflate(context, R.layout.exam_history_item, null);
			vh = new ViewHolder();
			vh.tv1 = (TextView) convertView.findViewById(R.id.exam_history_tv1);
			vh.tv2 = (TextView) convertView.findViewById(R.id.exam_history_tv2);
			vh.tv3 = (TextView) convertView.findViewById(R.id.exam_history_tv3);
			vh.tv4 = (TextView) convertView.findViewById(R.id.exam_history_tv4);
			vh.tv5 = (TextView) convertView.findViewById(R.id.exam_history_tv5);
			vh.tv6 = (TextView) convertView.findViewById(R.id.exam_history_tv6);
			vh.tv7 = (TextView) convertView.findViewById(R.id.exam_history_tv7);
			vh.tv8 = (TextView) convertView.findViewById(R.id.exam_history_tv8);
			vh.tv9 = (TextView) convertView.findViewById(R.id.exam_history_tv9);
			vh.tv10 = (TextView) convertView.findViewById(R.id.exam_history_tv10);
			vh.tv11 = (TextView) convertView.findViewById(R.id.exam_history_tv11);
			vh.timeTv = (TextView) convertView.findViewById(R.id.exam_history_time);
			TextView[] tvs = { vh.tv1, vh.tv2, vh.tv3, vh.tv4, vh.tv5, vh.tv6, vh.tv7, vh.tv8, vh.tv9, vh.tv10, vh.tv11,
					vh.timeTv };
			if (BluetoothConstant.XUEYA_TYPENAME.equals(flag)) {
				showText(4, tvs);
			} else if (BluetoothConstant.NIAOCHANGGUI_TYPENAME.equals(flag)) {
				showText(12, tvs);
			} else if (BluetoothConstant.TIWEN_TYPENAME.equals(flag)) {
				//体温  显示类型
				showText(3, tvs);
			} else {
				showText(2, tvs);
			}
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		if (BluetoothConstant.XUEYA_TYPENAME.equals(flag)) {
			if (gaodata != null) {
				vh.tv1.setText(gaodata.getCL_VALUE());
			} else {
				vh.tv1.setText("");
			}
			if (didata != null) {
				vh.tv2.setText(didata.getCL_VALUE());
			} else {
				vh.tv2.setText("");
			}

			if (xinlvdata != null) {
				vh.tv3.setText(xinlvdata.getCL_VALUE());
			} else {
				vh.tv3.setText("");
			}
			if (gaodata != null) {
				vh.timeTv.setText(gaodata.getCL_TIME());
			} else {
				vh.timeTv.setText("");
			}

		} else if (BluetoothConstant.NIAOCHANGGUI_TYPENAME.equals(flag)) {
			if (niaodanyuan != null) {
				String cl_VALUE = niaodanyuan.getCL_VALUE();
				if (!TextUtils.isEmpty(cl_VALUE)) {
					vh.tv1.setText(cl_VALUE);
					if (cl_VALUE.contains("+")) {
						setArrowUp(vh.tv1, true);
					}
				} else {
					vh.tv1.setText("");
				}

			} else {
				vh.tv1.setText("");
			}

			if (qianxue != null) {
				String cl_VALUE2 = qianxue.getCL_VALUE();
				if (!TextUtils.isEmpty(cl_VALUE2)) {
					vh.tv2.setText(cl_VALUE2);
					if (cl_VALUE2.contains("+")) {
						setArrowUp(vh.tv2, true);
					}
				} else {
					vh.tv2.setText("");
				}
			} else {
				vh.tv2.setText("");
			}

			if (danhongsu != null) {
				String cl_VALUE3 = danhongsu.getCL_VALUE();
				if (!TextUtils.isEmpty(cl_VALUE3)) {
					vh.tv3.setText(cl_VALUE3);
					if (cl_VALUE3.contains("+")) {
						setArrowUp(vh.tv3, true);
					}
				} else {
					vh.tv3.setText("");
				}
			} else {
				vh.tv3.setText("");
			}

			if (tongti != null) {
				String cl_VALUE4 = tongti.getCL_VALUE();
				if (!TextUtils.isEmpty(cl_VALUE4)) {
					vh.tv4.setText(cl_VALUE4);
					if (cl_VALUE4.contains("+")) {
						setArrowUp(vh.tv4, true);
					}
				} else {
					vh.tv4.setText("");
				}
			} else {
				vh.tv4.setText("");
			}

			if (putaotang != null) {
				String cl_VALUE5 = putaotang.getCL_VALUE();
				if (!TextUtils.isEmpty(cl_VALUE5)) {
					vh.tv5.setText(cl_VALUE5);
					if (cl_VALUE5.contains("+")) {
						setArrowUp(vh.tv5, true);
					}
				} else {
					vh.tv5.setText("");
				}
			} else {
				vh.tv5.setText("");
			}

			if (danbaizhi != null) {
				String cl_VALUE6 = danbaizhi.getCL_VALUE();
				if (!TextUtils.isEmpty(cl_VALUE6)) {
					vh.tv6.setText(cl_VALUE6);
					if (cl_VALUE6.contains("+")) {
						setArrowUp(vh.tv6, true);
					}
				} else {
					vh.tv6.setText("");
				}
			} else {
				vh.tv6.setText("");
			}

			if (pH != null) {
				String cl_VALUE7 = pH.getCL_VALUE();
				if (!TextUtils.isEmpty(cl_VALUE7)) {
					vh.tv7.setText(cl_VALUE7);
					if (Double.parseDouble(cl_VALUE7) > 8) {
						setArrowUp(vh.tv7, true);
					} else if (Double.parseDouble(cl_VALUE7) < 4.6) {
						setArrowUp(vh.tv7, false);
					}
				} else {
					vh.tv7.setText("");
				}

			} else {
				vh.tv7.setText("");
			}

			if (yaxiao != null) {
				String cl_VALUE8 = yaxiao.getCL_VALUE();
				if (!TextUtils.isEmpty(cl_VALUE8)) {
					vh.tv8.setText(cl_VALUE8);
					if (cl_VALUE8.contains("+")) {
						setArrowUp(vh.tv8, true);
					}
				} else {
					vh.tv8.setText("");
				}

			} else {
				vh.tv8.setText("");
			}

			if (baixibao != null) {
				String cl_VALUE9 = baixibao.getCL_VALUE();
				if (!TextUtils.isEmpty(cl_VALUE9)) {
					vh.tv9.setText(cl_VALUE9);
					if (cl_VALUE9.contains("+")) {
						setArrowUp(vh.tv9, true);
					}
				} else {
					vh.tv9.setText("");
				}
			} else {
				vh.tv9.setText("");
			}

			if (bizhong != null) {
				String cl_VALUE10 = bizhong.getCL_VALUE();
				if (!TextUtils.isEmpty(cl_VALUE10)) {
					vh.tv10.setText(cl_VALUE10);
					if (cl_VALUE10.contains(">=")) {
						cl_VALUE10 = cl_VALUE10.substring(2);
					}
					if (Double.parseDouble(cl_VALUE10) >= 1.025) {
						setArrowUp(vh.tv10, true);
					} else if (Double.parseDouble(cl_VALUE10) < 1.015) {
						setArrowUp(vh.tv10, false);
					}
				} else {
					vh.tv10.setText("");
				}
			} else {
				vh.tv10.setText("");
			}

			if (weishengsu != null) {
				String cl_VALUE11 = weishengsu.getCL_VALUE();
				if (!TextUtils.isEmpty(cl_VALUE11)) {
					vh.tv11.setText(cl_VALUE11);
					if (cl_VALUE11.contains("+")) {
						setArrowUp(vh.tv11, true);
					}
				} else {
					vh.tv11.setText("");
				}
			} else {
				vh.tv11.setText("");
			}

			if (weishengsu != null) {
				String cl_TIME = weishengsu.getCL_TIME();
				if (!TextUtils.isEmpty(cl_TIME)) {
					vh.timeTv.setText(cl_TIME);
				} else {
					vh.timeTv.setText("");
				}
			} else {
				vh.timeTv.setText("");
			}

		} else if (BluetoothConstant.TIWEN_TYPENAME.equals(flag)) {
			//体温  显示类型
			
			if (data != null) {
				vh.tv1.setText(data.getCL_TYPE());
				vh.tv2.setText(data.getCL_VALUE());
				vh.timeTv.setText(data.getCL_TIME());
				vh.timeTv.setBackground(null);
			} else {
				vh.tv1.setText("");
				vh.tv2.setText("");
				vh.timeTv.setText("");
			}
 
		}  else {
			if (data != null) {
				Log.e(TAG, data.toString());
				Log.e(TAG, "data.getCL_VALUE()" + data.getCL_VALUE());
				vh.tv1.setText(data.getCL_VALUE());
				vh.timeTv.setText(data.getCL_TIME());
			} else {
				vh.tv1.setText("");
				vh.timeTv.setText("");
			}
		}
		return convertView;
	}

	class ViewHolder {
		TextView tv1;
		TextView tv2;
		TextView tv3;
		TextView tv4;
		TextView tv5;
		TextView tv6;
		TextView tv7;
		TextView tv8;
		TextView tv9;
		TextView tv10;
		TextView tv11;
		TextView timeTv;
	}

	private void showText(int count, TextView[] tvs) {
		if (count < 1 || count > 12) {
			return;
		}
		for (TextView textView : tvs) {
			if (textView.getVisibility() == View.VISIBLE) {
				textView.setVisibility(View.GONE);
			}
		}

		for (int i = 0; i < count - 1; i++) {
			TextView textView = tvs[i];
			if (textView.getVisibility() != View.VISIBLE) {
				textView.setVisibility(View.VISIBLE);
			}
		}
		tvs[tvs.length - 1].setVisibility(View.VISIBLE);

	}

	private void setArrowUp(TextView tv, boolean isUp) {
		if (isUp) {
			Drawable upArrow = context.getResources().getDrawable(R.drawable.hongse);
			upArrow.setBounds(-10, 0, upArrow.getMinimumWidth() / 2 - 10, upArrow.getMinimumHeight() / 2);
			tv.setCompoundDrawables(null, null, upArrow, null);
		} else {
			Drawable downArrow = context.getResources().getDrawable(R.drawable.lanse);
			downArrow.setBounds(-10, 0, downArrow.getMinimumWidth() / 2 - 10, downArrow.getMinimumHeight() / 2);
			tv.setCompoundDrawables(null, null, downArrow, null);
		}
	}

}
